package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.BusinessException;
import com.xuecheng.base.exception.CommonError;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.service.CourseTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {

    /**
     * 获取课程教师列表
     * @param courseId
     * @return
     */
    @Override
    public List<CourseTeacher> getTeacherListByCourseId(long courseId) {
        //参数校验
        if (courseId <= 0) {
            BusinessException.cast(CommonError.PARAMS_ERROR);
        }
        //查询
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId,courseId);
        return list(queryWrapper);
    }


    /**
     * 添加教师到课程
     * @param courseTeacher
     * @return
     */
    @Override
    public CourseTeacher addTeacherToCourse(CourseTeacher courseTeacher) {
        if (courseTeacher == null) {
            BusinessException.cast(CommonError.REQUEST_NULL);
        }
        //参数校验
        if (StringUtils.isBlank(courseTeacher.getTeacherName())){
            throw new BusinessException("教师名称不能为空");
        }
        if (StringUtils.isBlank(courseTeacher.getPosition())){
            throw new BusinessException("教师职位不能为空");
        }
        //该课程的教师名不能重复
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId,courseTeacher.getCourseId())
                .eq(CourseTeacher::getTeacherName,courseTeacher.getTeacherName());
        CourseTeacher teacher = getOne(queryWrapper);
        if (teacher!=null) throw new BusinessException("教师信息重复!");
        //添加
        boolean save = save(courseTeacher);
        if (!save) BusinessException.cast(CommonError.UNKOWN_ERROR);
        return courseTeacher;
    }

    /**
     * 修改教师信息
     * @param courseTeacher
     * @return
     */
    @Override
    public CourseTeacher updateTeacherInfo(CourseTeacher courseTeacher) {
        if (courseTeacher == null) {
            BusinessException.cast(CommonError.REQUEST_NULL);
        }
        Long id = courseTeacher.getId();
        if (id == null || id <= 0) {
            BusinessException.cast(CommonError.PARAMS_ERROR);
        }
        //查询教师信息是否存在
        CourseTeacher teacher = getById(id);
        if (teacher==null) BusinessException.cast(CommonError.QUERY_NULL);
        //修改
        boolean res = updateById(courseTeacher);
        if (!res) BusinessException.cast(CommonError.UNKOWN_ERROR);
        return courseTeacher;
    }

    /**
     * 删除教师
     * @param courseId
     * @param id
     */
    @Override
    public void deleteTeacher(long courseId, long id) {
        if (courseId <= 0 || id <= 0) {
            BusinessException.cast(CommonError.PARAMS_ERROR);
        }
        CourseTeacher teacher = getById(id);
        if (teacher==null) BusinessException.cast(CommonError.QUERY_NULL);
        boolean res = removeById(id);
        if (!res)BusinessException.cast(CommonError.UNKOWN_ERROR);
    }
}
